//index.js
//获取应用实例
var app = getApp()
Page({
  //data信息调用
  // var that = this;
  // console.log('data信息:', that.data.imgUrls)
    data: {
        inputShowed: false,
        inputVal: "",
        activeIndex: 0,
        sliderOffset: 0,
        sliderLeft: 0,
        imgUrls: [
            'http://img02.tooopen.com/images/20150928/tooopen_sy_143912755726.jpg',
            'http://img06.tooopen.com/images/20160818/tooopen_sy_175866434296.jpg',
            'http://img06.tooopen.com/images/20160818/tooopen_sy_175833047715.jpg'
        ],
        indicatorDots: true,
        autoplay: true,
        interval: 2000,
        duration: 1000,
        circular: true,
        //   歌曲搜索的结果
        searchReault: [],
        songData:[],
		//   歌曲推荐
		recommendSongs: []
    },
    showInput: function () {
        this.setData({
            inputShowed: true
        });
    },
    hideInput: function () {
        this.setData({
            inputVal: "",
            inputShowed: false
        });
    },
    clearInput: function () {
        this.setData({
            inputVal: ""
        });
    },
    // 搜索歌曲
    inputTyping: function (e) {
      let that = this
      console.log(e.detail)
      this.setData({
        inputVal: e.detail.value
      });
      wx.request({
        url: 'http://neteasemusic.leanapp.cn/search',
       //url: 'http://localhost:3000/search',
        data: {
          keywords: e.detail.value
        },
        method: 'GET',
        success: function (res) {
          let temp = []
          if (!res.data.result.songs) {
            return;
          }
          res.data.result.songs.forEach((song, index) => {

            temp.push({
              id: song.id,
              name: song.name,
              mp3Url: song.mp3Url,
              picUrl: song.album.picUrl,
              singer: song.artists[0].name
            })
            that.setData({
              searchReault: temp
            })


          })
          // 存入搜索的结果进缓存
          wx.setStorage({
            key: "searchReault",
            data: temp
          })
        },
        fail: function (res) {
          // fail
        },
        complete: function (res) {
          // complete

        }
      })
    },

    //根据歌曲id获取歌曲Url
    getDetailMusicInfos: function (musicid) {

      wx.request({
        method: 'GET',
        url: 'http://localhost:3000/music/url?id=' + musicid + '',
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success: (res) => {
          // 把返回的歌曲列表设定为list中的数据
          this.setData({
            clickedSongUrl: res.data.data[0].url,
          });
        },
        error: () => {
          console.log('err');
        }
      });
    },
   
    onShow: function () {
      wx.hideLoading();


      let that = this


      wx.request({
        url: 'http://localhost:3000/personalized/newsong',
        data: {

        },
        method: 'GET',
        success: function (res) {
          let temp = []
          if (!res.data.result) {
            return;
          }
          console.log('result', res.data.result)
          res.data.result.forEach((song, index) => {

            console.log('song:', song)
            console.log('picUrl', song.song.album.picUrl)
            temp.push({
              id: song.id,
              name: song.name,
              //mp3Url: song.mp3Url,
              picUrl: song.song.album.picUrl,
              singer: song.song.artists[0].name
            })
            that.setData({
              recommendSongs: temp
            })


          })
          // 存入搜索的结果进缓存
          wx.setStorage({
            key: "recommendSongs",
            data: temp
          })
        },
        fail: function (res) {
          // fail
        },
        complete: function (res) {
          // complete
        }
      })

    },
    onLoad: function () {
        var that = this;
        console.log('data信息:', that.data.imgUrls)
        wx.getSystemInfo({
            success: function (res) {
                that.setData({
                    sliderOffset: res.windowWidth 
                });
            }
        });
    },
    tabClick: function (e) {
        this.setData({
            sliderOffset: e.currentTarget.offsetLeft,
            activeIndex: e.currentTarget.id
        });
    },
    tonow: function (event) {
      let that = this
      wx.request({
        url: 'http://localhost:3000/music/url',
        data: {
          id: event.currentTarget.dataset.id
        },
        method: 'GET',
        success: function (res) {
          let songData = {
            id: event.currentTarget.dataset.id,
            name: event.currentTarget.dataset.name, 
            mp3Url: res.data.data[0].url,
            picUrl: event.currentTarget.dataset.picurl,
            singer: event.currentTarget.dataset.singer
          }
          that.setData({
           songData: songData
          })
          // 将当前点击的歌曲保存在缓存中
          app.globalData.clickedSong_global = songData
          wx.setStorageSync('clickdata', songData)
        },
        fail: function (res) {
          // fail
        },
        complete: function (res) {
          // complete
          wx.switchTab({
            url: '../now/index',
          })
        }
      })
    }
});
